package com.lzy.mall.ware.config;

import com.lzy.mall.common.constant.MQConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lzy
 * @date 2021/10/11 14:41
 */
@Configuration
public class WareMQConfig {

//    @Autowired
//    AmqpAdmin amqpAdmin;

    /**
     * 库存服务交换机
     *
     * @return
     */
    @Bean
    public Exchange stockEventExchange() {
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(MQConstant.stockEventExchange, true, false);
    }

    @Bean
    public Queue stockReleaseStockQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete, @Nullable Map<String, Object> arguments
        return new Queue(MQConstant.stockReleaseQueue, true, false, false);
    }

    @Bean
    public Queue stockDelayQueue() {
        //设置属性
        Map<String, Object> arguments = new HashMap<>();
        arguments.put(MQConstant.XDLExchange, MQConstant.stockEventExchange);
        arguments.put(MQConstant.XDLRK, MQConstant.stockReleaseRK);
        arguments.put(MQConstant.XMTTL, 120000);
        return new Queue(MQConstant.stockDelayQueue, true, false, false, arguments);
    }

//    @PostConstruct
//    public void createStockDelayQueue() {
//        //设置属性
//        Map<String, Object> arguments = new HashMap<>();
//        arguments.put(MQConstant.XDLExchange, MQConstant.stockEventExchange);
//        arguments.put(MQConstant.XDLRK, MQConstant.stockReleaseRK);
//        arguments.put(MQConstant.XMTTL, 120000);
//        Queue queue = new Queue(MQConstant.stockDelayQueue, true, false, false, arguments);
//        amqpAdmin.declareQueue(queue);
//    }

    @Bean
    public Binding stockLockedBinding() {
        //tring destination, Binding.DestinationType destinationType, String exchange, String routingKey, @Nullable Map<String, Object> arguments
        return new Binding(MQConstant.stockDelayQueue,
                Binding.DestinationType.QUEUE,
                MQConstant.stockEventExchange,
                MQConstant.stockLockRK,
                null);
    }


    @Bean
    public Binding stockReleaseBinding() {
        //tring destination, Binding.DestinationType destinationType, String exchange, String routingKey, @Nullable Map<String, Object> arguments
        return new Binding(MQConstant.stockReleaseQueue,
                Binding.DestinationType.QUEUE,
                MQConstant.stockEventExchange,
                MQConstant.stockReleaseRK + ".#",
                null);
    }
}
